package main

import (
	"fmt"
	"io"
	"log"
	"os"
)

type _myLogger *log.Logger

type _myLog struct {
	Logger _myLogger
}

var (
	InfoLog *log.Logger
	WarnLog *log.Logger
	ErrLog  *log.Logger
	CronLog *log.Logger
)

const (
	InfoLogFile = "info.log"
	WarnLogFile = "warn.log"
	ErrLogFile  = "error.log"
	CronLogFile = "cron.log"
)

//set log flags
func LogInit() {
	var errFile, warnFile, infoFile, cronFile *os.File
	var err error

	if infoFile, err = os.OpenFile(InfoLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
		log.Println("Failed to log to file, using default stderr")
	}
	if warnFile, err = os.OpenFile(WarnLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
		log.Println("Failed to log to file, using default stderr")
	}
	if errFile, err = os.OpenFile(ErrLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
		log.Println("Failed to log to file, using default stderr")
	}
	if cronFile, err = os.OpenFile(CronLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
		log.Println("Failed to log to file, using default stderr")
	}

	InfoLog = log.New(io.MultiWriter(infoFile), "[INFO] ", log.Ldate|log.Ltime|log.Lshortfile)
	WarnLog = log.New(io.MultiWriter(warnFile, infoFile), "[WARN] ", log.Ldate|log.Ltime|log.Lshortfile)
	ErrLog = log.New(io.MultiWriter(os.Stdout, errFile, warnFile, infoFile), "[ERROR] ", log.Ldate|log.Ltime|log.Lshortfile)
	CronLog = log.New(io.MultiWriter(cronFile), "[CRON] ", log.Ldate|log.Ltime|log.Lshortfile)
}

func main() {
	LogInit()
	InfoLog.Printf("OrcaFS-API info log init success")
	WarnLog.Printf("OrcaFS-API info log init success")
	ErrLog.Printf("OrcaFS-API info log init success")
	CronLog.Printf("OrcaFS-API cron log init success")
	var MyLog _myLog
	MyLog.Logger = _myLogger(InfoLog)
	MyLog.Errorf("aaaaaa")
}

func (l _myLog) Errorf(format string, v ...interface{}) {
	((*log.Logger)(l.Logger)).SetPrefix("[ERROR]")
	((*log.Logger)(l.Logger)).Output(2, fmt.Sprintf(format, v...))
	((*log.Logger)(l.Logger)).SetPrefix("")
}
